<?php

    // Form submitted?
    if ( !empty( $_POST['submitted'] ) )
    {
        // Init arrays.
        $settings = array(
            'events'  => array( ),
            'system'  => array( ),
        );
        $accepted = array(
            'events'  => array( ),
            'systems' => array( ),
        );
        $files = array(
            'events'  => array( ),
            'systems' => array( ),
        );

        // Get settings.
        foreach ( $_POST as $key => $value )
        {
            // Is event accepted?
            if ( ( substr( $key, 0, 2 ) !== "eP" ) &&
                 ( substr( $key, 0, 1 ) === "e"  ) &&
                 ( $value === "on" ) )
            {
                // Save.
                $accepted['events'] []= substr( $key, 1 );
            }

            // Is event?
            if ( substr( $key, 0, 2 ) === "eP" )
            {
                // Save.
                $files['events'][ substr( $key, 2 ) ] = $value;
            }

            // Is system accepted?
            if ( ( substr( $key, 0, 2 ) !== "sP" ) &&
                 ( substr( $key, 0, 1 ) === "s"  ) &&
                 ( $value === "on" ) )
            {
                // Save.
                $accepted['systems'] []= substr( $key, 1 );
            }

            // Is system?
            if ( substr( $key, 0, 2 ) === "sP" )
            {
                // Save.
                $files['systems'][ substr( $key, 2 ) ] = $value;
            }
        }

        // Walk through accepted event.
        foreach ( $accepted['events'] as $index )
        {
            // Add to settings.
            $settings['events'] []= $files['events'][ $index ];
        }

        // Walk through accepted system.
        foreach ( $accepted['systems'] as $index )
        {
            // Add to settings.
            $settings['system'] []= $files['systems'][ $index ];
        }

        // Collect admin info.
        $settings['admin']['full_name'] = $_POST['full_name'];
        $settings['admin']['username']  = $_POST['username'];
        $settings['admin']['email']     = $_POST['email'];

        // Password match?
        if ( $_POST['password'] === $_POST['password2'] )
        {
            // Encrypt password.
            $settings['admin']['password'] = sha1( $_POST['password'] );

            // Compose file.
            $data  = "<?\n";
            $data .= "#" . serialize( $settings );
            $data .= "\n?>";

            // Save changes.
            if ( file_put_contents( "data/System.php", $data ) )
            {
                // Redirect.
                header( "HTTP/1.0 303 See Other" );
                header( "Location: " . $_SERVER['PHP_SELF'] );
            }

            // No changes?
            else
            {
                // Print error.
                ?><div class="error">Unable to save changes.</div><?php
            }
        }

        // Password mismatch?
        else
        {
            // Print error.
            ?><div class="error">Passwords do not match.</div><?php
        }
    }

    // Init settings.
    $settings = array(
        'events' => array( ),
        'system' => array( ),
    );

    // Data file exists?
    if ( file_exists( "data/System.php" ) )
    {
        // Read contents.
        if ( $contents = file_get_contents( "data/System.php" ) )
        {
            // Parse contents.
            $contents = explode( "\n", $contents );

            // Extra data.
            $contents = substr( trim( $contents[1] ), 1 );

            // Unserialize data.
            $settings = unserialize( $contents );
        }
    }

    // No admin?
    if ( empty( $settings['admin'] ) )
    {
        // Populate.
        $settings['admin'] = array(
            'full_name' => "",
            'username'  => "",
            'password'  => "",
            'email'     => "",
        );
    }

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Mukei Data System Data Editor</title>
  <style type="text/css">
      body, div {
        color: #000000;
        font-family: Arial, Helvetica, sans-serif;
        font-size: 11px;
        margin: 5px;
    }
    .error {
        border: 1px solid #FF0000;
        color: #FF0000;
        font-weight: bold;
        padding: 4px 6px 4px 6px;
    }
    .container {
        border: 1px solid #CCCCCC;
        padding: 4px 6px 4px 6px;
    }
    .container .header {
        border-bottom: 1px solid #CCCCCC;
        color: #666666;
        font-size: 24px;
        font-weight: bold;
    }
    .container .form {
        padding: 5px 0px 5px 0px;
    }
    .container .form form {
        margin: 0px;
        padding: 0px;
    }
    .container .form .option {
        clear: both;
    }
    .container .form .option .checkbox {
        float: left;
        width: 25px;
    }
    .container .form .option .name {
        padding-top: 3px;
        width: 300px;
    }
    .container .form .submit input {
        clear: both;
        font-size: 11px;
        margin-top: 10px;
        width: 45px;
    }
    html>body .container .form .submit input {
        clear: both;
        font-size: 11px;
        margin-left: -22px;
        margin-top: 10px;
        width: 45px;
    }
    .container .footer {
        border-top: 1px solid #CCCCCC;
        color: #999999;
        font-weight: bold;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="header">Mukei System Data Editor</div>
    <div class="form">
      <form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
      <input type="hidden" name="submitted" value="what do you think" />
      <h3>Local Admin Credentials</h3>
      <p>In case a membership or database system plugin has not been installed, a temporary local administrative account may be used.</p>
      <div class="option">
        <div class="title">Full Name:</div>
        <div class="text"><input type="text" name="full_name" value="<?= $settings['admin']['full_name'] ?>" /></div>
      </div>
      <div class="option">
        <div class="title">Username:</div>
        <div class="text"><input type="text" name="username" value="<?= $settings['admin']['username'] ?>" /></div>
      </div>
      <div class="option">
        <div class="title">Password:</div>
        <div class="text"><input type="password" name="password" /></div>
      </div>
      <div class="option">
        <div class="title">Password (Confirm):</div>
        <div class="text"><input type="password" name="password2" /></div>
      </div>
      <div class="option">
        <div class="title">E-mail:</div>
        <div class="text"><input type="text" name="email" value="<?= $settings['admin']['email'] ?>" /></div>
      </div>
      <h3>Event Plugins</h3>
      <p>This is a list of event plugins located in the events directory.</p>
<?php

        // Scan events.
        $events = special_scan( "events" );

        // Array empty?
        if ( empty( $events ) )
        {
?>
      <p><b>No event plugins found.</b></p>
<?php
        }

        foreach ( $events as $index => $event )
        {
            // Init flag.
            $flag = false;

            // Is allowed?
            if ( in_array( $event, $settings['events'] ) )
                $flag = true;

?>
      <div class="option">
        <input type="hidden" name="eP<?= $index ?>" value="<?= urlencode( $event ) ?>" />
        <div class="checkbox"><input type="checkbox" name="e<?= $index ?>" id="e<?= $index ?>" <?= $flag ? "checked" : "" ?>/></div>
        <div class="name"><label for="e<?= $index ?>"><b><?= $event ?></b> <?php

            // Allowed?
            if ( $flag )
            {
                ?><font color="#00FF00">(Allowed)</font><?php
            }

            // Not allowed?
            else
            {
                ?><font color="#FF0000">(Not Allowed)</font><?php
            }

?></label></div>
      </div>
<?php
        }

?>
      <h3>System Plugins</h3>
      <p>This is a list of system plugins allowed to execute at startup.</p>
<?php

        // Scan events.
        $system = special_scan( "system" );

        // Array empty?
        if ( empty( $system ) )
        {
?>
      <p><b>No system plugins found.</b></p>
<?php
        }

        foreach ( $system as $index => $plugin )
        {
            // Init flag.
            $flag = false;

            // Is allowed?
            if ( in_array( $plugin, $settings['system'] ) )
                $flag = true;

?>
      <div class="option">
        <input type="hidden" name="sP<?= $index ?>" value="<?= urlencode( $plugin ) ?>" />
        <div class="checkbox"><input type="checkbox" name="s<?= $index ?>" id="s<?= $index ?>" <?= $flag ? "checked=\"\"" : "" ?>/></div>
        <div class="name"><label for="s<?= $index ?>"><b><?= $plugin ?></b> <?php

            // Allowed?
            if ( $flag )
            {
                ?><font color="#00FF00">(Allowed)</font><?php
            }

            // Not allowed?
            else
            {
                ?><font color="#FF0000">(Not Allowed)</font><?php
            }

?></label></div>
      </div>
<?php
        }

?>
      <div class="submit"><input type="submit" value="Save" /></div>
      </form>
    </div>
    <div class="footer">Copyright &copy 2007 Kevin Herrera.</div>
  </div>
</body>

</html><?php

    function special_scan ( $dir = "" )
    {
        // Get extension.
        $ext = array_pop( explode( ".", basename( __FILE__ ) ) );
        $ext = preg_quote( $ext, "/" );

        // Scan directory.
        $contents = scandir( $dir );

        // Remove extras.
        array_shift( $contents );
        array_shift( $contents );

        // Remove non-PHP.
        foreach ( $contents as $idx => $content )
        {
            // Not?
            if ( !preg_match( "/$ext/", $content ) )
            {
                // Remove.
                unset( $contents[ $idx ] );
            }
        }

        return( array_values( $contents ) );
    }

?>